package com.atguigu.lease.web.admin.service.impl;

import com.atguigu.lease.model.enums.BaseStatus;
import com.atguigu.lease.web.admin.vo.user.UserInfoQueryDto;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.atguigu.lease.model.entity.UserInfo;
import com.atguigu.lease.web.admin.service.UserInfoService;
import com.atguigu.lease.web.admin.mapper.UserInfoMapper;
import org.springframework.stereotype.Service;

import java.util.List;
import java.util.stream.Collectors;

/**
 * @author gqs
 * @description 针对表【user_info(用户信息表)】的数据库操作Service实现
 * @createDate 2023-07-24 15:48:00
 */
@Service
public class UserInfoServiceImpl extends ServiceImpl<UserInfoMapper, UserInfo>
    implements UserInfoService {

  @Override
  public IPage<UserInfo> pageUserInfo(IPage<UserInfo> page, UserInfoQueryDto queryVo) {
    IPage<UserInfo> pageUserInfoList =
        this.page(
            page,
            Wrappers.lambdaQuery(UserInfo.class)
                .eq(queryVo.getStatus() != null, UserInfo::getStatus, queryVo.getStatus())
                .eq(queryVo.getPhone() != null, UserInfo::getPhone, queryVo.getPhone()));
    List<UserInfo> userInfoList = pageUserInfoList.getRecords();
    // stream流批量设置用户信息密码为null
    List<UserInfo> updatedUserInfoList =
        userInfoList.stream()
            .peek(user -> user.setPassword(null)) // peek用于修改元素
            .collect(Collectors.toList()); // 收集修改后的元素到新的列表中
    // 更新page对象中的记录
    pageUserInfoList.setRecords(updatedUserInfoList);
    return pageUserInfoList;
  }

  @Override
  public boolean updateStatusByUserId(Long id, BaseStatus status) {
    boolean flag =
        this.update(
            Wrappers.lambdaUpdate(UserInfo.class)
                .eq(UserInfo::getId, id)
                .set(UserInfo::getStatus, status));
    return flag;
  }
}
